﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DictionaryCatch {
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window {
	string dirPath = AppDomain.CurrentDomain.BaseDirectory + "dictionary";
	int tableSize = 20;
	public int GetRandomSeed ( ) {
		byte[] bytes = new byte[4];
		System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider ( );
		rng.GetBytes ( bytes );
		return BitConverter.ToInt32 ( bytes, 0 );
	}
	public MainWindow ( ) {
		InitializeComponent ( );
		this.Loaded += new RoutedEventHandler ( this.initCanvas );
		if (!Directory.Exists ( dirPath )) {
			Directory.CreateDirectory ( dirPath );
		}
		DirectoryInfo TheFolder = new DirectoryInfo ( dirPath );
		foreach (FileInfo nf in TheFolder.GetFiles ( )) {
			if (nf.FullName.EndsWith ( ".txt" )) {
				SelDic.Items.Add ( nf.Name);
			}
		}
	}
	Label[, ] LMartix;
	private void initCanvas ( object sender, RoutedEventArgs e ) {
		LMartix = new Label[tableSize, tableSize];
		Console.WriteLine ( "初始化canvas" );
		for (int i = 0; i < tableSize; i++) {
			for (int j = 0; j < tableSize; j++) {
				Label r = new Label ( );
				r.Width = Canvas.ActualWidth / tableSize;
				r.Height = Canvas.ActualHeight / tableSize;
				r.SetValue ( Canvas.TopProperty, i * r.Height );
				r.SetValue ( Canvas.LeftProperty, j * r.Width );
				//r.Content = "空";
				r.HorizontalContentAlignment =HorizontalAlignment.Center;
				r.VerticalContentAlignment = VerticalAlignment.Center;
				r.FontSize=14;
				Canvas.Children.Add ( r );
				LMartix[i, j] = r;
			}
		}
	}
	private void ClearData ( ) {
		if (t != null) {
			t.Abort ( );
		}
		if (dictionary!=null ) {
			dictionary.Clear ( );
		}
		if (dicFile != null) {
			dicFile.Close ( );
		}
		if (CharSets != null) {
			CharSets.Clear ( );
		}
	}
	Thread t;
	Dictionary<string, string> dictionary;
	StreamReader dicFile;
	List<char> CharSets;
	private void load_Click ( object sender, RoutedEventArgs e ) {
		ClearData ( );

		t = new Thread ( ( ) => {
			if (dictionary != null) {
				dictionary.Clear ( );
			}
			dictionary = new Dictionary<string, string > ( );
			CharSets = new List<char> ( );
			Dispatcher.Invoke ( new Action ( ( ) => {
				if(SelDic.SelectedItem!=null)
					dicFile = new StreamReader ( dirPath + "\\" + SelDic.SelectedItem, Encoding.Default );
			} ) );

			string key;
			string value;
			int valueC=0;
			if (dicFile != null) {
				while ((key = dicFile.ReadLine ( )) != null&& (value = dicFile.ReadLine ( )) != null) {
					if (dictionary.ContainsKey ( key )||key.Contains('-')|| key.Contains ( ' ' )|| key.Contains ( '/' )|| key.Contains ( '.' ))
						continue;
					dictionary.Add ( key, value );
					valueC++;
					foreach (char c in key) {
						if (!CharSets.Contains ( c ))
							CharSets.Add ( c );
					}
					if (valueC % 1000 == 0) {
						Dispatcher.Invoke ( new Action ( ( ) => {
							loadingMes.Foreground = Brushes.Red;
							loadingMes.Content = "正在读取:" + dictionary.Keys.Last ( ) + "\n当前第" + value.ToString ( ) + "个";
						} ) );
					}
				}
				Dispatcher.Invoke ( new Action ( ( ) => {
					loadingMes.Foreground = Brushes.Green;
					loadingMes.Content = "读取完毕。共" + valueC.ToString ( ) + "条记录";
				} ) );
			} else {
				Dispatcher.Invoke ( new Action ( ( ) => {
					loadingMes.Foreground = Brushes.Red;
					loadingMes.Content = "请先选择一个有效的词典";
				} ) );
			}


		} );
		t.Start ( );
	}
	string top, topRight, right, rightBottom, bottom, bottomLeft, left, topLeft;

	private void Window_SizeChanged ( object sender, SizeChangedEventArgs e ) {
		if(LMartix!=null)
			for (int i = 0; i < tableSize; i++) {
				for (int j = 0; j < tableSize; j++) {
					Label r = LMartix[i, j];
					r.Width = Canvas.ActualWidth / tableSize;
					r.Height = Canvas.ActualHeight / tableSize;
					r.SetValue ( Canvas.TopProperty, i * r.Height );
					r.SetValue ( Canvas.LeftProperty, j * r.Width );
				}
			}
	}

	private void randChars_Click ( object sender, RoutedEventArgs e ) {
		new Thread ( ( ) => {
			if(CharSets!=null)
				for (int i = 0; i < tableSize; i++) {
					for (int j = 0; j < tableSize; j++) {
						Dispatcher.Invoke (new Action(()=> {
							LMartix[i, j].Content = CharSets.ElementAt ( new Random ( GetRandomSeed ( ) ).Next ( 0, CharSets.Count ( ) - 1 ) );
						} ) );
					}
				}
		} ).Start ( );
	}
	private void StartFind_Click ( object sender, RoutedEventArgs e ) {
		new Thread ( ( ) => {
			Dispatcher.Invoke ( new Action ( ( ) => {
				listBox.Items.Clear ( );
			} ) );
			for (int i = 0; i < tableSize; i++) {
				for (int j = 0; j < tableSize; j++) {
					Dispatcher.Invoke ( new Action ( ( ) => {
						top = topRight = right = rightBottom = bottom = bottomLeft = left = topLeft = LMartix[i, j].Content.ToString ( );
					} ) );
					for (int length = 1; length <= tableSize; length++) {
						Dispatcher.Invoke ( new Action ( ( ) => {
							if (i - length >= 0)
								top += LMartix[i - length, j].Content.ToString ( );
							if (i - length >= 0 && j + length < tableSize)
								topRight += LMartix[i - length, j + length].Content.ToString ( );
							if (j + length < tableSize)
								right += LMartix[i, j + length].Content.ToString ( );
							if (i + length < tableSize && j + length < tableSize)
								rightBottom += LMartix[i + length, j + length].Content.ToString ( );
							if (i + length < tableSize && j < tableSize)
								bottom += LMartix[i + length, j].Content.ToString ( );
							if (i + length < tableSize && j - length >= 0)
								bottomLeft += LMartix[i + length, j - length].Content.ToString ( );
							if (j - length >= 0)
								left += LMartix[i, j - length].Content.ToString ( );
							if (i - length >= 0 && j - length >= 0)
								topLeft += LMartix[i - length, j - length].Content.ToString ( );


							string getValue;
							if (!listBox.Items.Contains ( top ) && dictionary.ContainsKey ( top )) {
								listBox.Items.Add ( top );
								if(dictionary.TryGetValue(top,out getValue))
									listBox.Items.Add ( getValue);
							}

							if (!listBox.Items.Contains ( topRight ) && dictionary.ContainsKey ( topRight )) {
								listBox.Items.Add ( topRight );
								if (dictionary.TryGetValue ( topRight, out getValue ))
									listBox.Items.Add ( getValue );
							}

							if (!listBox.Items.Contains ( right ) && dictionary.ContainsKey ( right )) {
								listBox.Items.Add ( right );
								if (dictionary.TryGetValue ( right, out getValue ))
									listBox.Items.Add ( getValue );
							}

							if (!listBox.Items.Contains ( rightBottom ) && dictionary.ContainsKey ( rightBottom )) {
								listBox.Items.Add ( rightBottom );
								if (dictionary.TryGetValue ( rightBottom, out getValue ))
									listBox.Items.Add ( getValue );
							}
							if (!listBox.Items.Contains ( bottom ) && dictionary.ContainsKey ( bottom )) {
								listBox.Items.Add ( bottom );
								if (dictionary.TryGetValue ( bottom, out getValue ))
									listBox.Items.Add ( getValue );
							}
							if (!listBox.Items.Contains ( bottomLeft ) && dictionary.ContainsKey ( bottomLeft )) {
								listBox.Items.Add ( bottomLeft );
								if (dictionary.TryGetValue ( bottomLeft, out getValue ))
									listBox.Items.Add ( getValue );
							}
							if (!listBox.Items.Contains ( left ) && dictionary.ContainsKey ( left )) {
								listBox.Items.Add ( left );
								if (dictionary.TryGetValue ( left, out getValue ))
									listBox.Items.Add ( getValue );
							}
							if (!listBox.Items.Contains ( topLeft ) && dictionary.ContainsKey ( topLeft )) {
								listBox.Items.Add ( topLeft );
								if (dictionary.TryGetValue ( topLeft, out getValue ))
									listBox.Items.Add ( getValue );
							}




							if (listBox.Items.Count != 0) {
								listCount.Foreground = Brushes.Green;
								listCount.Content = "已匹配到" + listBox.Items.Count + "条记录。";
							} else {
								listCount.Foreground = Brushes.Red;
								listCount.Content = "当前随机表无法匹配到任何记录" ;
							}

						} ) );
					}
				}
			}
		} ).Start ( );
	}
}
}
